package org.example.school.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.school.entity.Teachers;
import org.example.school.entity.Users;
import org.example.school.mapper.TeachersMapper;
import org.example.school.mapper.UsersMapper;
import org.example.school.model.dto.TeacherProfileDTO;
import org.example.school.service.ITeachersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 教师信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-10-09
 */
@Service
public class TeachersServiceImpl extends ServiceImpl<TeachersMapper, Teachers> implements ITeachersService {

    @Autowired
    private TeachersMapper teachersMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public TeacherProfileDTO getTeacherProfile(Long userId) {
        // 1. 获取教师基本信息
        Teachers teacher = this.getOne(new LambdaQueryWrapper<Teachers>().eq(Teachers::getUserId, userId));
        if (teacher == null) return null;

        // 2. 获取用户通用信息 (电话等)
        Users user = usersMapper.selectById(userId);

        // 3. 组装DTO
        TeacherProfileDTO dto = new TeacherProfileDTO();
        dto.setFullName(teacher.getFullName());
        dto.setGender(teacher.getGender());
        if (user != null) {
            dto.setPhoneNumber(user.getPhoneNumber());
        }

        // 4. 查询并设置任教课程信息
        List<TeacherProfileDTO.CourseInfo> courses = teachersMapper.findTeachingCoursesByTeacherId(teacher.getId());
        dto.setTeachingCourses(courses);

        return dto;
    }
}

